package red.stu.pconstraint.dto.mapper;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;

/**
 * 流程 Mapper
 * @author yihrmc @stu.red
 * @date 2019/11/15 9:01
 */
@Getter
@Setter
@ToString
public class ProcessMapper {

    /**
     * 命名空间 (processId) <br/>
     *  - 需要保持全局唯一 <br/>
     *  - 在模糊搜索pcc文件时，如果命名空间指定的是一个类名，则自动绑定
     */
    @NotNull
    private String namespace;

    /**
     * 所有流程分支
     */
    @NotNull
    private Map<String, Branch> branchs;

    /**
     * 主流程分支
     */
    @NotNull
    private Branch masterBranch;

    /**
     * 流程描述信息
     */
    @Nullable
    private String describe;

    public Branch getBranch(String branchId) {
        Branch branch = this.getBranchs().get(branchId);
        if (branch == null) {
            throw new RuntimeException("Branch not find: " + branchId);
        }
        return branch;
    }

    public BranchNode getBranchNode(String branchId, String nodeId) {
        BranchNode branchNode = getBranch(branchId).getNodes().get(nodeId);
        if (branchNode == null) {
            throw new RuntimeException("BranchNode not find: " + branchId + ">" + nodeId);
        }
        return branchNode;
    }

}
